import { AccessLevel, Inject, SingletonProto } from "@eggjs/tegg";
import { BaseController } from "@/module/common";
import { EggLogger } from "typings/app";


@SingletonProto({
    accessLevel: AccessLevel.PUBLIC
})
export class GroupMesService extends BaseController {

    @Inject()
    logger: EggLogger

    @Inject()
    eggContextHandler: any;

    init() {
        this.eggContextHandler.app.axios.defaults.timeout = 0
    }

    /**
     * 发送自定义机器人消息
     * @param postUrl 自定义机器人地址
     * @param content 消息体
     * @param type 消息类型
     * @returns 
     */
    async SendGroupMes(postUrl: string, content: any = "测试", type: string = "text") {
        if (!postUrl) {
            return this.error("请传递群组机器人地址")
        }
        let mapSet = new Map()
            .set("interactive", { "card": content })
            .set("text", {
                "content": {
                    "text": content
                }
            })
            .set("post", {
                "content": {
                    "post": content
                }
            })
            .set('image', {
                "content": {
                    "image_key": content
                }
            })
            .set("share_chat", {
                "content": {
                    "share_chat_id": content
                }
            })

        if (!mapSet.has(type)) {
            return this.error("未定义消息类型")
        }
        let res = "";


        try {
            res = await this.eggContextHandler.app.axios.post(postUrl, { "msg_type": type, ...mapSet.get(type) })
        } catch (error) {
            return this.error("群消息发送失败！")
        }
        return this.success(res)
    }
}